// 路由

// 先引入vue
import Vue from 'vue'
// 再引入vue-router
import VueRouter from 'vue-router'

// 使用路由懒加载优化页面打开速度，打包构建项目时JavaScript包会变得非常大，影响页面加载，所以需要使用路由懒加载进行优化

// 引入login、home、welcome
// import Login from '../components/Login.vue'
// import Home from '../components/Home.vue'
// import Welcome from '../components/Welcome.vue'

// import("路由分组，将来打包到同一个文件中","路由路径")
const Login = () => import(/* webpackChunkName: "Login_Home_Welcome" */ '../components/Login.vue')
const Home = () => import(/* webpackChunkName: "Login_Home_Welcome" */ '../components/Home.vue')
const Welcome = () => import(/* webpackChunkName: "Login_Home_Welcome" */ '../components/Welcome.vue')

// 引入users、rights、roles
// import Users from '../components/users/Users.vue'
// import Rights from '../components/power/Rights.vue'
// import Roles from '../components/power/Roles.vue'
const Users = () => import(/* webpackChunkName: "Users_Rights_Roles" */ '../components/users/Users.vue')
const Rights = () => import(/* webpackChunkName: "Users_Rights_Roles" */ '../components/power/Rights.vue')
const Roles = () => import(/* webpackChunkName: "Users_Rights_Roles" */ '../components/power/Roles.vue')

// 引入cate、params
// import Cate from '../components/goods/Cate.vue'
// import Params from '../components/goods/Params.vue'
const Cate = () => import(/* webpackChunkName: "Cate_Params" */ '../components/goods/Cate.vue')
const Params = () => import(/* webpackChunkName: "Cate_Params" */ '../components/goods/Params.vue')

// 引入list、Add
// import GoodsList from '../components/goods/List.vue'
// import Add from '../components/goods/Add.vue'
const GoodsList = () => import(/* webpackChunkName: "GoodsList_Add" */ '../components/goods/List.vue')
const Add = () => import(/* webpackChunkName: "GoodsList_Add" */ '../components/goods/Add.vue')

// 引入order、report
// import Order from '../components/order/Order.vue'
// import Report from '../components/report/Report.vue'
const Order = () => import(/* webpackChunkName: "Order_Report" */ '../components/order/Order.vue')
const Report = () => import(/* webpackChunkName: "Order_Report" */ '../components/report/Report.vue')

// 在vue中使用VueRouter
Vue.use(VueRouter)

// 在路由实例对象中挂载路由规则
const router = new VueRouter({
  // 路由规则
  routes: [
    { path: '/', redirect: '/login' },
    { path: '/login', component: Login },
    // 路由嵌套，实现首页嵌套子路由，展示欢迎页
    {
      path: '/home',
      component: Home,
      redirect: '/welcome',
      children: [
        { path: '/welcome', component: Welcome },
        { path: '/users', component: Users },
        { path: '/rights', component: Rights },
        { path: '/roles', component: Roles },
        { path: '/categories', component: Cate },
        { path: '/params', component: Params },
        { path: '/goods', component: GoodsList },
        { path: '/goods/add', component: Add },
        { path: '/orders', component: Order },
        { path: '/reports', component: Report }
      ]
    }
  ]
})

// 为路由对象挂载beforeEach路由导航守卫
// 防止用户未登录状态下对其他API页面访问成功
router.beforeEach((to, from, next) => {
  // to 将要访问的路径
  // form 代表从哪个路径跳转而来
  // next 是一个函数，代表放行
  //   next() 放行，next('/login') 强制跳转

  // 如果访问的是登录页，则直接放行
  if (to.path === '/login') return next()
  // 如果访问的其他页面，获取token，如果token不存在，则强制跳转回登录页
  const tokenStr = window.sessionStorage.getItem('token')
  if (!tokenStr) return next('/login')
  // token存在，则直接放行
  next()
})

// 默认导出路由实例对象
export default router
